package com.keyidea.jfxui.core.server.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * UDPClient类
 */
@Slf4j
public class UdpClient {

    public static void sendMsg(DatagramSocket socket, String ip, int port, byte[] buf) {
        try {
            DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getByName(ip), port);
            socket.send(packet);
            log.debug("send msg from <{}:{}>", socket.getLocalAddress().getHostAddress(), socket.getPort());
            log.debug("send msg to <{}:{}/{} -- [{}]>", ip, port, buf.length, ByteUtils.bytesToHex(buf));
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

}
